﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace A1
{
    class Ball
    {
        private Rectangle rec;  //The rectangle of the ball used for collision detection
        private Vector2 speed;  //The speed of the ball as an XY vector pair
        private Texture2D sprite;   //The sprite of the ball
        private bool fourPlayer;    //whether we have four players
        private int GameHeight; //the game height
        private int GameWidth; //The game width
        private Paddle lastHit; //The last hit paddle
        private int leftScore; //The left paddle's score
        private int rightScore; //The right paddle's score
        private int bottomScore; //The bottom paddle's score
        private int topScore; //The top paddle's score
        private bool charge; //Whether we are in charge mode
        private bool curve; //Whether to curve the ball


        public bool Curve
        {
            get { return curve; }
            set { curve = value; }
        }
        /// <summary>
        /// Whether we're in charge mode
        /// </summary>
        public bool Charge
        {
            get { return charge; }
            set { charge = value; }
        }
        /// <summary>
        /// Returns whether we are fourplayer or not
        /// </summary>
        public bool FourPlayer
        {
            get { return fourPlayer; }
            set { fourPlayer = value; }
        }
        /// <summary>
        /// Returns the left paddles score
        /// </summary>
        public int LeftScore
        {
            get { return leftScore; }
            set { leftScore = value; }
        }
        /// <summary>
        /// Returns the right paddles score
        /// </summary>
        public int RightScore
        {
            get { return rightScore; }
            set { rightScore = value; }
        }

        /// <summary>
        /// The top paddle's score
        /// </summary>
        public int TopScore
        {
            get { return topScore; }
            set { topScore = value; }
        }

        /// <summary>
        /// The bottom paddle's score
        /// </summary>
        public int BottomScore
        {
            get { return bottomScore; }
            set { bottomScore = value; }
        }

        /// <summary>
        /// The last paddle to collide with the ball
        /// </summary>
        public Paddle LastHit
        {
            get { return lastHit; }
            set { lastHit = value; }
        }

        /// <summary>
        /// The bounding rectangle of the ball
        /// </summary>
        public Rectangle Rec
        {
            get { return rec; }
        }

        /// <summary>
        /// Ther sprite of the ball
        /// </summary>
        public Texture2D Sprite
        { get { return sprite; } }

        /// <summary>
        /// The speed of the ball in the X direction
        /// </summary>
        public float SpeedX
        {
            get { return speed.X; }
            set { speed.X = value; }
        }
        /// <summary>
        /// The speed of the ball in the Y direction
        /// </summary>
        public float SpeedY
        {
            get { return speed.Y; }
            set { speed.Y = value; }
        }

        /// <summary>
        /// Create a new ball
        /// </summary>
        /// <param name="gh">The game height</param>
        /// <param name="gw">The game width</param>
        /// <param name="s">The texture of the ball</param>
        /// <param name="ch">Whether we are in charge mode</param>
        /// <param name="four">Wehter we have four players</param>
        public Ball(int gh, int gw, Texture2D s, bool ch, bool four)
        {
            rec = new Rectangle(gw / 2, gh / 2, 10, 10); speed = new Vector2(3f, -1f); sprite = s; fourPlayer = true;
            GameWidth = gw; GameHeight = gh; lastHit = null;

            charge = false;
            leftScore = 10;
            rightScore = 10;
            topScore = 10;
            bottomScore = 10;

            /*ball.Charge != true && ball.FourPlayer == true*/
        }

        /// <summary>
        /// Update the ball (movement)
        /// </summary>
        public void Update()
        {
            rec.Y += (int)SpeedY; rec.X += (int)SpeedX;


            if (fourPlayer)
            {
                Random rand = new Random();
                int xspeed = rand.Next(-5, 6);
                int yspeed = rand.Next(-5, 6);

                while (xspeed == 0)
                { xspeed = rand.Next(-5, 6); }

                while (xspeed == yspeed || yspeed == 0)
                { yspeed = rand.Next(-5, 6); }

                if (rec.X < 0)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;

                    if (charge && lastHit != null)
                    {
                        switch (lastHit.Position)
                        {
                            case Game1.Position.Left:
                                { break; }
                        }
                    }

                    leftScore--;

                }
                if (rec.Y < 0)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;

                    topScore--;

                }
                if (rec.Y + (rec.Top - rec.Bottom) > GameHeight)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;

                    bottomScore--;
                }
                if (rec.X + (rec.Right - rec.Left) > GameWidth)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;

                    rightScore--;
                }
            }
            else
            {
                Random rand = new Random();
                int xspeed = rand.Next(-5, 6);
                int yspeed = rand.Next(-5, 6);

                while (xspeed == yspeed)
                { yspeed = rand.Next(-5, 6); }

                if (rec.X < 0)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;
                }
                if (rec.X > GameWidth)
                {
                    rec.X = (GameWidth / 2) - (rec.Width / 2);
                    rec.Y = (GameHeight / 2) - (rec.Height / 2);
                    SpeedX = xspeed;
                    SpeedY = yspeed;
                }
                if (rec.Y < 0)
                { SpeedY *= -1; }
                if (rec.Y > GameHeight)
                { SpeedY *= -1; }
            }

        }
    }
}
